package com.ygnn.reggie.controller;

import com.alibaba.druid.util.StringUtils;
import com.aliyuncs.http.HttpRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ygnn.reggie.common.R;
import com.ygnn.reggie.entity.User;
import com.ygnn.reggie.service.UserService;
import com.ygnn.reggie.utils.SMSUtils;
import com.ygnn.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.text.StringContent;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        String phone= user.getPhone();
//         判断手机号（没什么必要）
        if (!StringUtils.isEmpty(phone)){
//            生成随机思维验证码
            String code= ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);
//            调用阿里云提供短信服务API完成发送短信
//            SMSUtils.sendMessage("小丁短信","null",phone,code);

//            将验证码保存到Session
            session.setAttribute(phone,code);

            return R.success("手机验证码发送成功");
        }

        return R.error("发送失败");
    }



    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpSession session){
        log.info(map.toString());

//        获取手机号
        String phone = map.get("phone").toString();

////        phone设置到session
//        session.setAttribute("user",phone);

//        获取code
        String code = map.get("code").toString();
//        获取session中的code
        Object sessionCode = session.getAttribute(phone);

        log.info("sessionCode:{}",sessionCode);
//        比对验证吗
        if(sessionCode!=null&&sessionCode.equals(code)){
//          查数据库
            LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);

            if (user!=null)
            {
                session.setAttribute("user", user.getId());
                return R.success(user);
            }
            else
            {
                User user1=new User();
                user1.setPhone(phone);
                userService.save(user1);
                session.setAttribute("user", user1.getId());
                return R.success(user1);
            }

        }
        return R.error("登录失败");
    }
    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request){
        //尝试删除
        try {
            request.getSession().removeAttribute("user");
        }catch (Exception e){
            //删除失败
            return R.error("登出失败");
        }
        return R.success("登出成功");
    }



}
